package com.android.task5;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.google.android.maps.GeoPoint;

public class ImageViewActivity extends Activity {
	
	Menu appMenu;
	
	final int MENU_NEXT_PHOTO = 1;
	final int MENU_PREVIOUS_PHOTO = 2;
	final int MENU_MAP_VIEW_MODE = 3;
	
	/**index to the current photo to be displayed */
	int currentPhoto = 0;
	ImageViewer imageViewer;
	boolean isFirst = true;
	
	/**FlickrManager to organize the Flickr requests */
	FlickrManager myFlickerManager;
	/**PanoramioManager to organize the Panoramio requests. */
	PanoramioManager myPanoramioManager;
	
	/**ArrayList holding the FlickrPhoto objects */
	ArrayList<FlickrPhoto> flickrPhotos;
	/**ArrayList holding the PanoramioPhoto objects */
	ArrayList<PanoramioPhoto> panoramioPhotos;
	   
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      RelativeLayout rl = new RelativeLayout(this);
      imageViewer = new ImageViewer(this);
      rl.addView(imageViewer,new RelativeLayout.LayoutParams
    		  (LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
      this.setContentView(rl);
      
      executeQueries();
    }
	
    /** Creates the menu items */
    public boolean onCreateOptionsMenu(Menu menu) {
    	Log.i("btn","HERE");
    	appMenu = menu;
    	
    	appMenu.add(0, MENU_NEXT_PHOTO, 0, "Next Photo");
    	appMenu.add(0, MENU_PREVIOUS_PHOTO, 0, "Previous Photo");
    	appMenu.add(0, MENU_MAP_VIEW_MODE, 0, "Show Map");
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_NEXT_PHOTO:
        	changePhoto(true);
            return true;
        case MENU_PREVIOUS_PHOTO:
        	changePhoto(false);
        	return true;
        case MENU_MAP_VIEW_MODE:
        	changeToMapView();
        	return true;
        }
        return false;
    }
    /**Change back to MapView */
    void changeToMapView(){
    	Intent intent = new Intent(this,LocationBasedPhotoBrowser.class);
    	startActivity(intent);
    }
    /**Change the photo beiing displayed.
     * @param nextPhoto boolean indicating if the next or the previous photo
     * is to be displayed */
    void changePhoto(boolean nextPhoto){
    	imageViewer.showImage(nextPhoto);
    }
    /** Creates the bounding box and starts the queries for photos */
    void executeQueries(){
    	//TODO: Get photos from Flickr
    	//TODO: Get photos from Panoramio
    	
    	Bundle mapClipping = getIntent().getExtras();
    	
    	// UNUSED: 
//    	int centerLatitude = mapClipping.getInt("centerLatitude");
//    	int centerLongitude = mapClipping.getInt("centerLongitude");
////	// GeoPoint representing the north boarder
//    	GeoPoint northPoint = new GeoPoint(northBoarder, centerLongitude);
//    	// GeoPoint representing the south boarder
//    	GeoPoint southPoint = new GeoPoint(southBoarder, centerLongitude);
//    	// GeoPoint representing the west boarder
//    	GeoPoint westPoint = new GeoPoint(centerLatitude, westBoarder);
//    	// GeoPoint representing the east boarder
//    	GeoPoint eastPoint = new GeoPoint(centerLatitude, eastBoarder);
    
    	int northBoarder = mapClipping.getInt("northBoarder");
    	int southBoarder = mapClipping.getInt("southBoarder");
    	int westBoarder = mapClipping.getInt("westBoarder");
    	int eastBoarder = mapClipping.getInt("eastBoarder");

    	// GeoPoint representing the top right corner
    	GeoPoint topRightCorner = new GeoPoint(northBoarder,eastBoarder);
    	// GeoPoint representing the bottom left corner
    	GeoPoint bottomLeftCorner = new GeoPoint(southBoarder, westBoarder);
    	
    	//BoundingBox
    	BoundingBox myBoundingBox = new BoundingBox(topRightCorner,bottomLeftCorner);
    	
    	FlickrManager myFlickerManager = new FlickrManager();
    	// Receive and process the XML response from FlickR
    	myFlickerManager.getXMLResponseForBoungindBox(myBoundingBox);
    	this.imageViewer.setFlickrManager(myFlickerManager);
    	this.flickrPhotos = myFlickerManager.getFlickrPhotos();
    	this.imageViewer.setFlickrPhotos(this.flickrPhotos);
    	
    	myPanoramioManager = new PanoramioManager();
    	// Receive and process the JSON response from Panoramio
    	myPanoramioManager.getJSONResponseWithinBoundingBox(myBoundingBox);
    	this.panoramioPhotos = myPanoramioManager.getPanoramioPhotos();
    	this.imageViewer.setPanoramioPhotos(this.panoramioPhotos);
    	
    	//Display the first photo
    	changePhoto(true);
    }

}
